import {defineConfig, loadEnv } from "vite";
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from "path";
import vue from "@vitejs/plugin-vue";
import { viteMockServe } from "vite-plugin-mock";

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
  // 获取各个环境下对应的变量
  let env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // Specify the source directory where your SVG files are located
        iconDirs: [path.resolve(process.cwd(), "./src/assets/icons")],
        // Specify the component name format
        symbolId: "icon-[dir]-[name]",
      }),
      viteMockServe({
        // mockPath: "mock",
        localEnabled: command === "serve", //保证开发阶段可以使用mock接口
      }),
    ],
    resolve: { alias: { "@": path.resolve("./src") } },
    // scss全局变量一个配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: `@import "@/styles/variable.scss";`,
        },
      },
    },
    // 代理跨域
    server: {
      port: 1130,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE, // 获取数据服务器地址的获取
          changeOrigin: true, // 需要代理跨域
          rewrite: (path) => path.replace(/^\/api/, " "), // 路径从写
        },
      },
    },
  };
});
